package com.jeeplus.cms.config.sitemesh;

import com.jeeplus.cms.util.CmsCacheUtils;
import com.jeeplus.modules.cms.site.entity.CmsSite;
import org.sitemesh.builder.SiteMeshFilterBuilder;
import org.sitemesh.config.ConfigurableSiteMeshFilter;

import java.util.List;

/**
 * siteMesh过滤器
 *
 * @author WuXilin
 */
public class WebSiteMeshFilter extends ConfigurableSiteMeshFilter {

    /**
     * cms路径前缀
     */
    private String cmsFrontPath;

    public WebSiteMeshFilter() {

    }

    public WebSiteMeshFilter(String cmsFrontPath) {
        this.cmsFrontPath = cmsFrontPath.startsWith("/") ? cmsFrontPath : "/" + cmsFrontPath;
    }

    /**
     * decoratorPath 第一个参数配置被模板装饰的页面  第二个参数为模板页
     *
     * @param builder
     */
    @Override
    protected void applyCustomConfiguration(SiteMeshFilterBuilder builder) {
        //设置模板装饰的请求，参数（拦截的请求（可以采用通配符方式），装饰该请求的装饰模板）
        //index是一个页面模板放在src/main/resource/templates/decorators目录下
        List<CmsSite> siteList = CmsCacheUtils.getSiteListByCache();

        //白名单，不进行过滤的请求
        builder
//                .addDecoratorPath("/f/", "/decorators/andia")
//                .addDecoratorPath("/f/*", "/decorators/andia")
                .addExcludedPath(cmsFrontPath + "/*/static/*")
                .addExcludedPath("/" + cmsFrontPath + "/static/*")
                //可设置不同请求用不同模板装饰
                .addDecoratorPath(cmsFrontPath + "/test4/", "/decorators/test4")
                .addDecoratorPath(cmsFrontPath + "/common/message", "/decorators/blank")
                .addExcludedPath("/static/*")
                .addExcludedPath("/doc.html")
                .addExcludedPath("/swagger-ui.html")
                .addExcludedPath("/swagger**/**")
                .addExcludedPath("/webjars/**")
                .addExcludedPath("/v2/**")
                .addExcludedPath("/doc.html/*")
                .addExcludedPath("/plugin/*")
                .addExcludedPath("/css/*")
                .addExcludedPath("/decorators/*");
        if (siteList != null) {
            siteList.forEach(site -> {
                builder.addDecoratorPath(cmsFrontPath + "/" + site.getId(), "/decorators/" + site.getTheme().getEnname());
                builder.addDecoratorPath(cmsFrontPath + "/" + site.getId() + "/*", "/decorators/" + site.getTheme().getEnname());
            });
        }
//       builder.addTagRuleBundles(new DivExtractingTagRuleBundle());  //用于添加自定义标签
    }
}
